import BaseModel from './.model';
import { Table, Column, DataType, HasMany, BelongsToMany } from 'sequelize-typescript';
import RoleModel from './role.model';
import RolePermissionModel from './role-permission.model';

@Table({ tableName: 'permissions' })
export default class PermissionModel extends BaseModel {
  /** 权限编码 */
  @Column({ type: DataType.STRING, comment: '权限编码' })
  code!: string;

  /** 权限名称 */
  @Column({ type: DataType.STRING, comment: '权限名称' })
  name!: string;

  /** 组件路径 */
  @Column({ type: DataType.STRING, allowNull: true, comment: '组件路径' })
  component?: string;

  /** 图标 */
  @Column({ type: DataType.STRING, allowNull: true, comment: '图标' })
  icon?: string;

  /** 权限路径 */
  @Column({ type: DataType.STRING, allowNull: true, comment: '权限路径' })
  path?: string;

  /** 父权限ID */
  @Column({ type: DataType.UUID, allowNull: true, comment: '父权限ID' })
  parentId?: string;

  /** 子权限 */
  @HasMany(() => PermissionModel, { foreignKey: 'parentId' })
  children?: PermissionModel[];

  /** 拥有该权限的角色 */
  @BelongsToMany(() => RoleModel, () => RolePermissionModel)
  roles?: RoleModel[];

  /** 排序 */
  @Column({ type: DataType.INTEGER, comment: '排序' })
  sortOrder!: number;

  /** 权限类型 */
  @Column({ type: DataType.ENUM('menu', 'api'), comment: '权限类型' })
  type!: 'menu' | 'api';
}
